package com.gxuwz.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gxuwz.core.model.Cols;
import com.gxuwz.core.model.Page;
import com.gxuwz.core.util.UidUtils;
import com.gxuwz.core.web.action.BaseAction;
import com.gxuwz.pojo.Form;
import com.gxuwz.pojo.SysUser;
import com.gxuwz.pojo.Topic;
import com.gxuwz.service.IDataService;
import com.gxuwz.service.IFormService;
import com.gxuwz.service.ITopicService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 连续跳转页面,以及综合数据获取
 * @author Administrator
 *
 */
public class MainAction extends BaseAction implements ModelDriven<Form>{

	protected static final String TOPIC_ADD_JSP = "/WEB-INF/page/form/form_topic_add.jsp"; 
	protected static final String TEMPLATE_TOPIC_JSP = "/WEB-INF/page/form/form_template_topic.jsp"; 
	protected static final String WRITER_ADD_JSP = "/WEB-INF/page/form/form_writer_add.jsp"; 
	protected static final String EDIT_JSP = "/WEB-INF/page/form/form_edit.jsp"; 

	@Autowired
	private IFormService formService;
	@Autowired
	private ITopicService topicService;
	@Autowired
	private IDataService dataService;
	
	private Page page;
	private Form form = new Form();

	private String json;
	
	

	public String toTopicAdd(){
		setForwardView(TOPIC_ADD_JSP);
		return SUCCESS;
	}
	
	/**
	 * 发布复核
	 * @return
	 */
	public void asRepeat(){
		//保存新表格数据
		form = formService.findById(form.getId());
		String oldName = "data_"+form.getId();
		List<Topic> oldTopic = topicService.findByProperty("formId", form.getFormId());
		List<Topic> topicList = topicService.findByProperty("formId", form.getFormId());
//		SysUser user = (SysUser)ActionContext.getContext().getSession().get("sysUser");
//		form.setUserId(user.getUserId());
//		form.setUserName(user.getUserName());
		form.setFormName(form.getFormName()+"(复核)");
		form.setId(null);
		form.setFormId(UidUtils.UID());
		form.setFormState(0);
		form.setFormType(1);
		form.setCreateTime(new Timestamp(new Date().getTime()));
		form.setIsDelete(0);
		form = formService.save(form);
		//复制topic
		for(Topic topic:topicList){
			topic.setId(null);
			topic.setFormId(form.getFormId());
			topic = topicService.save(topic);
		}
		//创建表
		dataService.createDB("data_"+form.getId(), topicList);
		//复制数据
		dataService.copyData("data_"+form.getId(), oldName, topicList, oldTopic);
		
		
		return;
	}
	
	/**
	 * 保存表格基础信息以后跳转到内容信息
	 * @return
	 */
	public String formToTopic(){
		//随机生成编号
		form.setFormId(UidUtils.UID());
		//创建者，创建时间，删除标记
//		SysUser user = (SysUser)ActionContext.getContext().getSession().get("sysUser");
//		form.setUserId(user.getUserId());
//		form.setUserName(user.getUserName());
		form.setCreateTime(new Timestamp(new Date().getTime()));
		form.setIsDelete(0);
		formService.save(form);
		setForwardView(TOPIC_ADD_JSP);
		return SUCCESS;
	}
	
	public String formToTopicByTemplate(){
		//随机生成编号
		Form oldForm = formService.findById(form.getId());
		Gson gson = new Gson();
		Topic tc = new Topic();
		tc.setFormId(oldForm.getFormId());
		tc.setIsDelete(0);
		json = gson.toJson(topicService.findByExample(tc));
		
		form.setId(null);
		form.setFormId(UidUtils.UID());
		//创建者，创建时间，删除标记
//		SysUser user = (SysUser)ActionContext.getContext().getSession().get("sysUser");
//		form.setUserId(user.getUserId());
//		form.setUserName(user.getUserName());
		form.setCreateTime(new Timestamp(new Date().getTime()));
		form.setIsDelete(0);
		form = formService.save(form);
		
		setForwardView(TEMPLATE_TOPIC_JSP);
		return SUCCESS;
	}
	
	
	/**
	 * 保存内容信息，创建表结构
	 * @return
	 */
	public void saveTopic(){
		Gson gson = new Gson();
		List<Topic> topics = gson.fromJson(json, new TypeToken<List<Topic>>(){}.getType());
		form = formService.findByProperty("formId", form.getFormId()).get(0);
		for(Topic tc : topics){
			if(tc.getIsDelete() == null){
				tc.setIsDelete(0);
			}
			tc.setFormId(form.getFormId());
			topicService.save(tc);
		}
		topics = topicService.findByProperty("formId", form.getFormId());
		if(form.getFormType() == 0){
			dataService.createDB("data_"+form.getId(), topics);
		}else if(form.getFormType() == 2){
			dataService.createDBT("data_"+form.getId(), topics);
		}
		
		return;
	}
	
	/**
	 * form_list跳转到修改页面，需要表格基础以及内容数据
	 * @return
	 */
	public String toEdit(){
		form = formService.findById(form.getId());
		Gson gson = new Gson();
		Topic tc = new Topic();
		tc.setFormId(form.getFormId());
		tc.setIsDelete(0);
		json = gson.toJson(topicService.findByExample(tc));
		setForwardView(EDIT_JSP);
		return SUCCESS;
	}
	/**
	 * 添加填写者
	 */
	public void addWriter(){
		Gson gson = new Gson();
		List<SysUser> writers = gson.fromJson(json, new TypeToken<List<SysUser>>(){}.getType());
		form = formService.findByProperty("formId", form.getFormId()).get(0);
		dataService.addWriter("data_"+form.getId(), writers);
		return;
	}
	
	/**
	 * 获得当前表格的填写人
	 * @return
	 */
	public void getWriter(){
		List<SysUser> userList = dataService.getWriter("data_"+form.getId());
		page = new Page();
		page.setCode(0);
		page.setData(userList);
		page.setCount(page.getData().size());

		reJson(page);
	}
	

	public Form getForm() {
		return form;
	}

	public void setForm(Form form) {
		this.form = form;
	}

	@Override
	public Form getModel() {
		// TODO Auto-generated method stub
		return form;
	}
	public String getJson() {
		return json;
	}
	public void setJson(String json) {
		this.json = json;
	}
	

	
	
	
}
